package com.normal.zbase.impl.base

import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleObserver
import androidx.viewbinding.ViewBinding
import com.didi.drouter.annotation.Router
import com.normal.zbase.impl.manager.StackManager

/**
 * Created by zsf on 2022/1/20 11:39
 * *******************
 * BaseActivity
 * *******************
 */
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() ,
    View.OnClickListener {

    val viewBinding: VB by lazy { viewBinding() }

    protected abstract fun viewBinding(): VB

    lateinit var baseActivity: AppCompatActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        baseActivity = this
        StackManager.getActivityManager().addActivity(this)
        if (this is LifecycleObserver) lifecycle.addObserver((this as LifecycleObserver))
        initView(savedInstanceState)
        initListener()
    }


    private fun initARouter() {
        try {
            if (this.javaClass.isAnnotationPresent(Router::class.java)) {
//                ARouter.getInstance().inject(this)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    protected open fun startActivity(cls: Class<*>?) {
        startActivity(newIntent(cls))
    }

    //---------------------------------------
    protected open fun newIntent(cls: Class<*>?): Intent? {
        return Intent(this, cls)
    }

    override fun onDestroy() {
        super.onDestroy()
        StackManager.getActivityManager().removeActivity(this)
    }

    //---------------------------------------
    protected open fun getDisplayMetrics(): DisplayMetrics {
        return resources.displayMetrics
    }

    protected open fun getWidthPixels(): Int {
        return getDisplayMetrics().widthPixels
    }

    protected open fun getHeightPixels(): Int {
        return getDisplayMetrics().heightPixels
    }

    override fun onClick(v: View) {}

    protected open fun initView(savedInstanceState: Bundle?) {}
    protected open fun initListener() {}


}